import Vue from 'vue'
export default {
  namespaced: true,
  state: () => {
    return {
      goodlist:
        [
          { "products": [], }
        ],
      gooddetail: [],
      

    }
  },
  getters: {
    allInfo(state) {
      let allNum = 0;
      let allPrice = 0;
      let allCheck = true;
      let checkNum = 0;
      let checkPrice = 0;
      let { goodlist } = state;
      goodlist[0].products.forEach(({ isStorecheck }) => {
        if (!isStorecheck) {
          allCheck = false
        }
      });
      goodlist[0].products.forEach(({ ischeck, num, nowPrice }) => {
        if (ischeck) {
          checkNum += num
          checkPrice += num * nowPrice
        }
        if (!ischeck) {
          allCheck = false
        }
      })
      return {
        allPrice, allNum, allCheck, checkNum, checkPrice
      }
    },
    filterList(state) {
      for (let i = 0; i < state.goodlist.length; i++) {
        return state.goodlist[i].products.filter(item => {
          return item.ischeck
        })
      }
    },

  },
  mutations: {
    goodDetailMut(state, good) {
      let { gooddetail } = state;
      Vue.set(gooddetail, 'ischeck', false)
      Vue.set(gooddetail, 'isStorecheck', false)
      
        gooddetail.push(good)
     
      // gooddetail.splice(0,1,good)
      console.log(gooddetail);
    },
    goodAddMut(state, good) {//新增购物车商品
      let { goodlist } = state;
      let len = goodlist[0].products.length;
      for (let i = 0; i < len; i++) {
        if (goodlist[0].products[i].id == good.id) {
           goodlist[0].products[i].num = goodlist[0].products[i].num+good.num
          return;
        }
      }

      Vue.set(good, 'ischeck', false)
      Vue.set(good, 'isStorecheck', false)
      goodlist[0].products.push(good)
      console.log(goodlist[0].products)
    },
    goodDelMut(state, i) {
      state.goodlist.splice(i, 1)
    },
    goodStoreMut(state, index) {//店铺选择控制

      state.goodlist[0].products[index].isStorecheck = !state.goodlist[0].products[index].isStorecheck
      console.log(state.goodlist[index]);
      if (state.goodlist[0].products[index].isStorecheck) {
        state.goodlist[0].products.forEach(item => {
          if (state.goodlist[0].products[index].storeId == item.storeId)
            item.ischeck = true
        })
      } else {
        state.goodlist[0].products.forEach(item => {
          item.ischeck = false
        })
      }

    },
    goodNumMut(state, { index, bool }) { //商品数量控制
      let { num } = state.goodlist[0].products[index]
      if (bool) {
        num > 1 ? state.goodlist[0].products[index].num-- : ''
        return;
      }
      state.goodlist[0].products[index].num++
    },
    goodCheckMut(state, index) { //单选控制
      state.goodlist[0].products[index].ischeck = !state.goodlist[0].products[index].ischeck
      state.goodlist[0].products.forEach(item => {
        if (item.ischeck == false) {
          item.isStorecheck = false
        } else {
          item.isStorecheck = true
        }
      })
      console.log(state.goodlist[0].products[index].ischeck);
    },
    goodAllCheckMut(state, bool) {
      //全选控制单选

      state.goodlist[0].products.forEach(item => {
        item.isStorecheck = !bool
      })
      state.goodlist[0].products.forEach(item => {
        item.ischeck = !bool
      })


    },
    goodDelCheckMut(state) {  //删除已选中的商品
      for (let i = state.goodlist.length - 1; i >= 0; i--) {
        if (state.goodlist[i].isStorecheck) {
          state.goodlist.splice(i, 1)
        }
        for (let j = state.goodlist[i].products.length - 1; j >= 0; j--) {
          if (state.goodlist[i].products[j].ischeck) {

            state.goodlist[i].products.splice(j, 1)
          }
        }

      }
    }
  }
}